﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using WebApp.Models;
using Metier;
using WebApp;
using Poco;


namespace WebApp.Controllers
{
    public class AccountController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(LogOnModelView login, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (Membership.ValidateUser(login.Email, login.Password))
                    {
                        FormsAuthentication.SetAuthCookie(login.Email, login.RememberMe);
                        //utilisateur en ligne
                        //GestionComptes.UtilisateurEnLigne(true, GestionComptes.Trouver(login.Email));


                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {


                            Utilisateur user = new Utilisateur();
                            GestionRole role = new GestionRole();
                            GestionUtilisateur account = new GestionUtilisateur();
                            try
                            {
                                user = account.Chercher(login.Email);
                            }
                            catch (Exception e)
                            {

                            }
                            FlashHelpers.FlashInfo(this, "Bonjour " + user.Email);

                            switch (role.GetRolesForUser(user.Email)[0])
                            {

                                case "Device":
                                    return RedirectToAction("Index", "Home");
                                case "Serveur":
                                    return RedirectToAction("Index", "Serveur");
                                case "Cuisinier":
                                    return RedirectToAction("Index", "Cuisinier");
                                case "Manager":
                                    return RedirectToAction("Index", "Manager");
                                default:
                                    FlashHelpers.FlashError(this, "Une erreur est survenue pendant la connexion au site.");
                                    return RedirectToAction("LogOn", "Account");

                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Email ou mot de passe incorect.");
                    }
                }
            }
            catch(Exception ex)
            {
                FlashHelpers.FlashError(this, "Une erreur est survenue pendant la connexion au site.");
            }
            

            // If we got this far, something failed, redisplay form
            return View(login);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }
    }
}
